﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Hosting;

namespace Wedn.TemplateEngine.UC
{
    public class TemplateVirtualFile : VirtualFile
    {
        private string path;
        private string content;
        private TemplatePathProvider tpp;

        public TemplateVirtualFile(string virtualPath, TemplatePathProvider provider)
            : base(virtualPath)
        {
            this.tpp = provider;
        }
        //public bool Exists
        //{
        //    get { return (content != null); }
        //}

        public override Stream Open()
        {
            using (StreamReader reader = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + "themes/default/templates/"+path))
            {
                string pageTemplate = reader.ReadToEnd();
                Stream stream = new MemoryStream();
                if (string.IsNullOrEmpty(pageTemplate))
                {
                    // Put the page content on the stream.
                    StreamWriter writer = new StreamWriter(stream);
                    writer.Write(pageTemplate);
                    writer.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                }
                return stream;
            }
        }
    }
}
